package com.foreveross.proxyip.exception;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;

/**
 * Simple validation methods
 * 用于异常参数的验证如： Validate.notEmpty(url, "Must supply a valid URL");
 * 
 */
public final class Validate {
	 private Validate() {}
	 
	    /**
	     * Validates that the object is not null
	     * @param obj object to test
	     */
	    public static void notNull(Object obj) {
	        if (obj == null)
	            throw new IllegalArgumentException("Object must not be null");
	    }
	    
	    /**
	     * Validates that the object is not null
	     * @param obj object to test
	     * @param msg message to output if validation fails
	     */
	    public static void notNull(Object obj, String msg) {
	        if (obj == null)
	            throw new IllegalArgumentException(msg);
	    }
	    
	    /**
	     * Validates that the string is not empty
	     * @param string the string to test
	     */
	    public static void notEmpty(String string) {
	        if (string == null || string.length() == 0)
	            throw new IllegalArgumentException("String must not be empty");
	    }
	    
	    /**
	     * 判断代理池是否为空
	     * @param list
	     */
	    public static void isNotNull(List list){
	    	if(!(list!=null && list.size()>0)){
	    		 throw new IllegalArgumentException("List must not be empty");
	    	}
	    }
	    /**
	     * 检验IP是否正确用于下载下IP后入数据库
	     * @param ip
	     * @return
	     */
	     public static boolean checkIP(String ip){
			if(StringUtils.isNotEmpty(ip)){
				if(ip.length()<9||ip.length()>23){
					return false;
				}
			 Pattern p = Pattern.compile("([\\d]{1,3}[\\.][\\d]{1,3}[\\.][\\d]{1,3}[\\.][\\d]{1,3}[:][\\d]{1,6})",Pattern.CASE_INSENSITIVE+Pattern.DOTALL);
			    Matcher m = p.matcher(ip);
			     if(m.find()){
			    	if(StringUtils.isNotEmpty( m.group(1))){
			    		return true;
			    	}
			    	return false;
			     }
			}
			     return false;
		}

}
